package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//这里是一个多表关联查询！
		return baseAttributeMapper.selectAttributeList(category1Id);
	}

	@Override
	public List<JSONObject> getBaseCategoryList() {
		// 声明集合
		List<JSONObject> categoryList = new ArrayList<>();
		//查询所有数据  select * from category_view;
		List<BaseCategoryView> baseCategoryViewList1 = baseCategoryViewMapper.selectList(null);
		// baseCategoryViewList 使用stream进行分组，分组key是category1Id
		Map<Long, List<BaseCategoryView>> categoryViewMap = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//拉姆达表达式口诀：1.复制小括号2.写死右箭头3.落地大括号
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = categoryViewMap.entrySet().iterator();
		while (iterator.hasNext()) {
			//获取entry
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//获取key
			Long category1Id = entry.getKey();
			List<BaseCategoryView> basecategoryViewList1 = entry.getValue();
			//创建一个一级分类对象
			JSONObject category1 = new JSONObject();
			category1.put("categoryId", category1Id);
			category1.put("categoryName", basecategoryViewList1.get(0).getCategory1Name());
			//获取二级分类数据
			Map<Long, List<BaseCategoryView>> categoryMap = basecategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long,List<BaseCategoryView>>> iterator1= categoryMap.entrySet().iterator();
			//创建一个二级分类集合对象
			ArrayList<JSONObject> category2childList = new ArrayList<>();
			while (iterator1.hasNext()){
				//获取二级分类对象
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> basecategoryViewList2 = entry1.getValue();
				//创建一个二级分类对象
				JSONObject category2 = new JSONObject();
				category2.put("categoryId", category2Id);
				category2.put("categoryName", basecategoryViewList2.get(0).getCategory2Name());
				//获取到三级分类集合并放入对象中
				List<JSONObject> category3List = basecategoryViewList2.stream().map(baseCategoryView -> {
					//创建一个三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//将三级分类数据放入二级分类对象中
				category2.put("categoryChild", category3List);
				category2childList.add(category2);

			}
			category1.put("categoryChild", category2childList);
			categoryList.add(category1);
		}
		return categoryList;
	}

	@Override
	public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {

		return baseCategoryViewMapper.selectById(category3Id);

	}

	@Override
	public JSONObject getAllCategoryList(Long category1Id) {
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
		// 声明一级分类对象
		JSONObject category1 = new JSONObject();
		category1.put("categoryId", category1Id);
		category1.put("categoryName", baseCategory1.getName());

		//获取全部分类信息
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));

		//根据二级分类ID分组转换数据
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		List<JSONObject> category2Child = new ArrayList<>();
		for(Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
			//二级分类ID
			Long category2Id = entry2.getKey();
			//二级分类对应的全部数据（三级数据）
			List<BaseCategoryView> category3List = entry2.getValue();

			// 声明二级分类对象
			JSONObject category2 = new JSONObject();
			category2.put("categoryId", category2Id);
			category2.put("categoryName", category3List.get(0).getCategory2Name());

			// 循环三级分类数据
			List<JSONObject> category3Child = new ArrayList<>();
			category3List.stream().forEach(category3View -> {
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", category3View.getCategory3Id());
				category3.put("categoryName", category3View.getCategory3Name());
				category3Child.add(category3);
			});
			category2Child.add(category2);
			// 将三级数据放入二级里面
			category2.put("categoryChild", category3Child);
		}
		// 将二级数据放入一级里面
		category1.put("categoryChild", category2Child);
		return category1;
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3ByCategory1Id(Long category1Id) {
//	select * from base_category3 where base_category3.category2_id in (101,102,103) and is_top = 1 limit 7;
		//	先根据一级分类Id 找到二级分类集合
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		//	查询置顶消息，每页显示7条数据；
		LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
		wrapper.in(BaseCategory3::getCategory2Id,category2IdList).eq(BaseCategory3::getIsTop,1).last("limit 7");
		return baseCategory3Mapper.selectList(wrapper);
	}

	@Override
	public List<BaseCategory1> findAllCategory1() {

		return baseCategory1Mapper.selectList(null);
	}
}
